$(document).ready(function() {
	//ajaxform
	var options = { 
		beforeSubmit:showRequest,//表单验证方式
        success:showResponse  // post-submit callback 
    }; 

    $('#ajaxForm').ajaxForm(options); 

    function showResponse(responseText, statusText)  {
		if (statusText == 'success') {
			var responseText=eval('('+responseText+')');
			if(responseText.state == 'SUCCESS'){
				_alert2('SUCCESS','operation success','');
				$("#name").val("");
				$("#tel").val("");
				$("#content").val("");
				$("#msg").html('');	
			}else{
				$("#form-btn").removeAttr('disabled');
		 		$("#msg").html(responseText.state);	
			}			
		}else{
			$("#form-btn").removeAttr('disabled');
		 	$("#msg").html('Lost Service');			
		}
	} 

	function showRequest(){

		 validata = $("form.form-validata").valid();
		 if (validata){
		 	$("#form-btn").attr('disabled','disabled');
		 	$("#msg").html('<img src="/tpl/Home/common/image/loadding.gif">');
		 }
		 return validata;
	}

	//validation验证
	$("form.form-validata").validate({
		errorElement: "p",
        errorClass: "error",
	})

	$.validator.addMethod("phone",function(value,element){
        return this.optional(element) || testPhone(value);
    },"Phone format error");

});

function testPhone(v){
    if(v==""){
        return false;
    }else{
        var pattern = /^((?:\d{9})|(?:\d{3}-\d{4})|(?:\d{3}-\d{3}-\d{4})|[0-9]{10})$/;
        flag = pattern.test(v);
        if(!flag){
            return false;
        }else{
            return true;
        }
    }
}

function _alert2(state,message,url){
    var formDialog = art.dialog({lock : true});
    if(state == 'SUCCESS'){
        formDialog.content(message);
        formDialog.title('SUCCESS');
        formDialog.icon('succeed');
        var timer,i=2;
        var fn = function () {
            formDialog.title(i + ' seconds after close');
            if (!i){
                formDialog.close();
                clearInterval(timer);
                if(url==''){
                    window.location.reload();
                }else{
                    window.location.href = url; 
                }
            }
            i --;
        };
        timer = setInterval(fn, 1000);
    }else{
        formDialog.content(state);
        formDialog.title('error message');
        formDialog.icon('error');
    }
}